######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Access Service
# -----------------------------------------------------------------
function New-AccesService()
{
    $obj=new-object PSObject -Property @{
      Name = "Access Service";
      Pool = New-ApplicationPool;
      Service = New-SupportingService;
      ApplicationLogSizeMax = 3000;
      CacheTimeout = 300;
      ColumnsMax = 30;
      NonRemotableQueriesAllowed = $false;
      OrderByMax = 4;
      OuterJoinsAllowed = $false;
      OutputCalculatedColumnsMax = 10;
      PrivateBytesMax = -1;
      RecordsInTableMax = 500000;
      RequestDurationMax = 30;
      RowsMax = 50000;
      SessionMemoryMax = 64;
      SessionsPerAnonymousUserMax = 10;
      SessionsPerUserMax = 10;
      SourcesMax = 8;
      TemplateSizeMax = -1;
    }
    $obj.Service.Name = "Access Database Service"
    
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Access Service"
        $this.Pool.Validate()
        $this.Service.Validate()
        Validate-NotEmpty "Name" $this.Name
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            
            $sa = New-SPAccessServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -ApplicationLogSizeMax $this.ApplicationLogSizeMax `
                    -CacheTimeout $this.CacheTimeout `
                    -ColumnsMax $this.ColumnsMax `
                    -OrderByMax $this.OrderByMax `
                    -OutputCalculatedColumnsMax $this.OutputCalculatedColumnsMax `
                    -PrivateBytesMax $this.PrivateBytesMax `
                    -RecordsInTableMax $this.RecordsInTableMax `
                    -RequestDurationMax $this.RequestDurationMax `
                    -RowsMax $this.RowsMax `
                    -SessionMemoryMax $this.SessionMemoryMax `
                    -SessionsPerAnonymousUserMax $this.SessionsPerAnonymousUserMax `
                    -SessionsPerUserMax $this.SessionsPerUserMax `
                    -SourcesMax $this.SourcesMax `
                    -TemplateSizeMax $this.TemplateSizeMax `
                    -Default `
                    -NonRemotableQueriesAllowed:$this.NonRemotableQueriesAllowed `
                    -OuterJoinsAllowed:$this.OuterJoinsAllowed
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
        $this.Service.Provision()
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
